﻿#include "BasicSceneContent.h"
#include "tools/ssProcessInfo.h"
#include "3dMath/ss3dMathFunction.h"
//#include "element/camera/ssThirdPersonCamera.h"
#include "element/render/ssMeshRender.h"
#include "element/material/ssMaterial.h"
#include "element/material/ssTextureMaterial.h"
#include "element/render/ssAxesRender.h"
//#include "element/render/ssColorRender.h"
#include "shader/ssShaderManager.h"
#include "console/ssConsole.h"
#include "node/ssNodeFactory.h"
#include "resmgr/ssResManager.h"


BasicSceneContent::BasicSceneContent(ssSceneManger& scene)
	:m_scene(scene)
{
	m_camera = 0;
	ssProcess::instance()->m_resPath = "../../res/";
}

BasicSceneContent::~BasicSceneContent(void)
{
}

void BasicSceneContent::InitNode()
{
	//f32 v[3 * 4] = { -10.0, 10.0, 0.0, -10.0, -10.0, 0.0, 10.0, -10.0, 0.0, 10.0, 10.0, 0.0 };
	//u16 f[3 * 2] = { 0, 1, 2, 0, 2, 3 };
	//StarSeeker::ssNode* pBallEn = new StarSeeker::ssNode("矩形");
	//StarSeeker::ssMeshRender* pRender = new StarSeeker::ssMeshRender();
	//StarSeeker::ssMaterial* pMaterial = new StarSeeker::ssMaterial();
	//pRender->SetVertex(3 * 4, v);
	//pRender->SetIndex(3 * 2, f);
	//StarSeeker::ssShader* ptestShader = StarSeeker::ssShaderManager::instance()->Find("base");
	//pMaterial->SetShader(ptestShader);
	//pBallEn->AddElement(pRender);
	//pBallEn->AddElement(pMaterial);
	//m_scene.RootNode().AddChild(pBallEn);

	f32 v[6*3]= {0.000000,0.000000,50.000000, -0.000002,50.000000,-0.000002, -50.000000,-0.000004,-0.000002, 0.000001,-50.000000,-0.000002, 50.000000,0.000009,-0.000002, 0.000000,0.000000,-50.000000};
	u16 f[8*3] = {0,1,2,0,2,3,0,3,4,0,4,1,5,2,1,5,3,2,5,4,3,5,1,4};
	f32 c[6*4] = {0.0,0.0,1.0,1.0, 0.0,1.0,0.0,1.0, 1.0,1.0,1.0,1.0, 1.0,1.0,1.0,1.0, 1.0,0.0,0.0,1.0, 1.0,1.0,1.0,1.0};
	StarSeeker::ssNode* pBallEn = new StarSeeker::ssNode("球体");
	StarSeeker::ssColorRender* pElement = new StarSeeker::ssColorRender();
	StarSeeker::ssMaterial* pMaterial = new StarSeeker::ssMaterial();
	pElement->SetVertex(6 * 3, v);
	pElement->SetIndex(8 * 3, f);
	pElement->SetColor(6 * 4, c);
	StarSeeker::ssShader* ptestShader = StarSeeker::ssShaderManager::instance()->Find("color");
	pMaterial->SetShader(ptestShader);
	pBallEn->AddComponent(pElement);
	pBallEn->AddComponent(pMaterial);
	m_scene.RootNode().AddChild(pBallEn);

	//四元数测试
	/////////////////////////////////和矩阵比较///////////////////////////////////
	//ssFLOAT3X axis1(64868, 541, 3245), axis2(9152, 5416, 59);
	//axis1.Normalize(); axis2.Normalize();
	//ssAngle angle1(AT_DEGREE), angle2(AT_DEGREE);
	//angle1 = 35.0f; angle2 = 123.0f;
	//ssFLOAT3x3X mat1, mat2, mat3;
	//Math3D::FLOAT3x3XRotation(&mat1, angle1.Radian(), &axis1);
	//Math3D::FLOAT3x3XRotation(&mat2, angle2.Radian(), &axis2);
	//mat3 = mat1*mat2;
	//ssFLOAT4X q1, q2, q3;
	//ssFLOAT3x3X tmat1, tmat2, tmat3;
	//q1.SetAxisAngle(axis1, angle1.Radian());
	//q2.SetAxisAngle(axis2, angle2.Radian());
	//Math3D::QuaternionToMatrix3x3(&q1, &tmat1);
	//Math3D::QuaternionToMatrix3x3(&q2, &tmat2);
	//q3 = q1.Cross(q2);
	//Math3D::QuaternionToMatrix3x3(&q3, &tmat3);
	////////////////////////////////矩阵转四元数//////////////////////////////////
	//ssFLOAT4X qTmp;
	//Math3D::Matrix3x3ToQuaternion(&mat1, &qTmp);
	////////////////////////////////四元数转矩阵//////////////////////////////////
	//angle1 = 45;
	//axis1.Set(0, 0, 1);
	//Math3D::FLOAT3x3XRotation(&mat1, angle1.Radian(), &axis1);
	//q1.SetAxisAngle(axis1, angle1.Radian());
	//Math3D::QuaternionToMatrix3x3(&q1, &tmat1);
	//q2.SetEuler(0, 0, angle1.Radian());
	//Math3D::QuaternionToMatrix3x3(&q2, &tmat2);
	////////////////////////////////四元数转轴角//////////////////////////////////
	//ssfloat a;
	//q1.GetAxisAngle(axis1, a);
	////////////////////////////////四元数转欧拉//////////////////////////////////
	//ssfloat x, y, z;
	//ssAngle rx(AT_RADIAN), ry(AT_RADIAN), rz(AT_RADIAN);
	//q2.GetEuler(x,y,z);
	//rx = x; ry = y; rz = z;
	//rx.UseDegree(); ry.UseDegree(); rz.UseDegree();
	//////////////////////////////四元数乘向量和矩阵比较////////////////////////////
	//ssFLOAT3X v1(985, 51, 6884); v1.Normalize();
	//ssFLOAT3X vret1, vret2;
	//Math3D::FLOAT3x3XTransformNormal(&vret1, &mat3, &v1);
	//Math3D::FLOAT4XTransformNormal(&vret2, &q3, &v1);
	////////////////////////////////////////////////////////////////////////////

	StarSeeker::ssNode* pAxesRE = SS_NODE_FACTORY->CreateAxesNode();
	m_scene.RootNode().AddChild(pAxesRE);
}

void BasicSceneContent::InitCamera()
{
	//ssFLOAT4x4X proj = StarSeeker::Math3D::OrthoRH((ssfloat)SS_OPENGL->m_nWidth, (ssfloat)SS_OPENGL->m_nHeight, 1.0f, 10000.0f);
	ssFLOAT4x4X proj = StarSeeker::Math3D::PerspectiveFovRH(45.0f, (ssfloat)SS_OPENGL->m_nWidth / (ssfloat)SS_OPENGL->m_nHeight, 1.0f, 10000.0f);
	ssNode* cameraNode1 = SS_NODE_FACTORY->CreateThirdPersonCamera();
	m_camera = SS_DYNAMIC_CAST(ssThirdPersonCamera*, cameraNode1->GetComponent(ssComponent::SS_COMPONENT_CAMERA));
	m_camera->SetProjMatrix(proj);
	m_camera->SetCoord(0.0f, 0.0f, 0.0f);
	m_scene.RootNode().AddChild(cameraNode1);
	m_camera->SetMainCamera();
}

void BasicSceneContent::OnMouseMove(i32 x, i32 y)
{
	m_camera->RotatingEye(x, y);
}

void BasicSceneContent::OnMouseWheel(i32 w)
{
	m_camera->ChangeFocus((ssfloat)w);
}

void BasicSceneContent::OnLeftUp(i32 x, i32 y)
{
}

ssCamera * BasicSceneContent::GetCamera()
{
	return m_camera;
}

void BasicSceneContent::OnKeyDown(i32 k)
{
	switch (k)
	{
		//case 65://a
		//	//m_pPlayerModel->Move(-40.f,0.f,0.f);
		//	//m_camera.SetCoord(m_pPlayerModel->m_coord.x,m_pPlayerModel->m_coord.y,m_pPlayerModel->m_coord.z);
		//	//CollisionTest();
		//	m_pPlayerModel->SetKeyState(1,0);
		//	break;
		//case 68://d
		//	//m_pPlayerModel->Move(40.f,0.f,0.f);
		//	//m_camera.SetCoord(m_pPlayerModel->m_coord.x,m_pPlayerModel->m_coord.y,m_pPlayerModel->m_coord.z);
		//	//CollisionTest();
		//	m_pPlayerModel->SetKeyState(1,1);
		//	break;
		//case 87://w
		//	//m_pPlayerModel->Move(0.f,40.f,0.f);
		//	//m_camera.SetCoord(m_pPlayerModel->m_coord.x,m_pPlayerModel->m_coord.y,m_pPlayerModel->m_coord.z);
		//	//CollisionTest();
		//	m_pPlayerModel->SetKeyState(1,2);
		//	break;
		//case 83://s
		//	//m_pPlayerModel->Move(0.f,-40.f,0.f);
		//	//m_camera.SetCoord(m_pPlayerModel->m_coord.x,m_pPlayerModel->m_coord.y,m_pPlayerModel->m_coord.z);
		//	//CollisionTest();
		//	m_pPlayerModel->SetKeyState(1,3);
		//	break;
		//case 81://q
		//	//m_pPlayerModel->Move(0.f,0.f,40.f);
		//	//m_camera.SetCoord(m_pPlayerModel->m_coord.x,m_pPlayerModel->m_coord.y,m_pPlayerModel->m_coord.z);
		//	//CollisionTest();
		//	m_pPlayerModel->SetKeyState(1,4);
		//	break;
		//case 69://e
		//	//m_pPlayerModel->Move(0.f,0.f,-40.f);
		//	//m_camera.SetCoord(m_pPlayerModel->m_coord.x,m_pPlayerModel->m_coord.y,m_pPlayerModel->m_coord.z);
		//	//CollisionTest();
		//	m_pPlayerModel->SetKeyState(1,5);
		//	break;
		//case 90://z
		//	m_pPlayerModel->SetKeyState(1,6);
		//	break;
		//case 88://x
		//	m_pPlayerModel->SetKeyState(1,7);
		//	break;
	case 82://r
		m_camera->ChangeFocus(40.f);
		break;
	case 70://f
		m_camera->ChangeFocus(-40.f);
		break;
	}
}

void BasicSceneContent::OnKeyUp(i32 k)
{
}
